import * as YvanUI from "yvan-ui-wotu";
import * as Data from "../../data";

const _ = YvanUI.lodash

interface Ref {
    theform: YvanUI.Form
}

export default class NodeInsert extends YvanUI.Scope<void, Ref> {

    main = this.$$({
        nodeType: 'disease',
        nodeValue: '',
        gender: '2',
        version: Data.defaultVersion,
        department: '',
        symptoms_group: '',
        diseases_group: '',
        guid: ''
    })

    onRender() {
        window['biz'] = this
    }

    vjson = {
        view: 'page',
        title: '知识节点',
        items: [
            {
                view: 'card',
                items: [
                    {
                        view: 'form',
                        ref: 'theform',
                        labelCol: {span: 6},
                        wrapperCol: {span: 14},
                        items: [
                            {
                                view: 'radio',
                                bind: 'main.nodeType',
                                label: '类型',
                                rules: [{required: true}],
                                optionType: 'button',
                                data: [
                                    // {value: 'symptom', label: '症状'},
                                    // {value: 'disease', label: '疾病'},
                                    {value: 'group', label: '组合'},
                                    // {value: 'department', label: '科室'},
                                    // {value: 'medicine', label: '药品'}
                                ]
                            },
                            {
                                view: 'text',
                                bind: 'main.nodeValue',
                                rules: [{required: true}],
                                visible: this.$watch(() => this.main.nodeType !== 'group'),
                                label: this.$watch(
                                    () => this.main.nodeType,
                                    (n) => {
                                        switch (n) {
                                            case 'symptom':
                                                return '症状名'
                                            case 'disease':
                                                return '疾病名'
                                            case 'department':
                                                return '科室名'
                                            case 'medicine':
                                                return '药品名'
                                        }
                                        return '未知'
                                    }
                                )
                            },
                            {
                                view: 'select',
                                bind: 'main.department',
                                rules: [{required: true}],
                                label: '科室',
                                visible: this.$watch(() => (this.main.nodeType === 'disease')),
                                dataSource: {
                                    func: () => {
                                        return new Promise(resolve => {
                                            YvanUI.ajax.func({
                                                url: '/kg/search/data/info',
                                                method: 'post-json',
                                                data: {
                                                    label: 'gender',
                                                    name: 'department',
                                                    align: false
                                                }
                                            }).then((res: any) => {
                                                console.log(res.result)
                                                const data = _.map(res.result, (i) => {
                                                    return {value: i, label: i}
                                                })
                                                resolve(data)
                                            }).finally(() => {

                                            })
                                        })
                                    }
                                },
                            },
                            {
                                view: 'select',
                                label: '症状组合',
                                bind: 'main.symptoms_group',
                                rules: [{required: true}],
                                mode: "multiple",
                                visible: this.$watch(() => this.main.nodeType === 'group'),
                                dataSource: {
                                    func: () => {
                                        return new Promise(resolve => {
                                            YvanUI.ajax.func({
                                                url: '/kg/search/data/info',
                                                method: 'post-json',
                                                data: {
                                                    label: 'gender',
                                                    name: 'symptom',
                                                    align: false
                                                }
                                            }).then((res: any) => {
                                                console.log(res.result)
                                                const data = _.map(res.result, (i) => {
                                                    return {value: i, label: i}
                                                })
                                                resolve(data)
                                            }).finally(() => {

                                            })
                                        })
                                    }
                                },
                            },
                            {
                                view: 'select',
                                label: '疾病组合',
                                bind: 'main.diseases_group',
                                rules: [{required: true}],
                                visible: this.$watch(() => this.main.nodeType === 'group'),
                                mode: "multiple",
                                dataSource: {
                                    func: () => {
                                        return new Promise(resolve => {
                                            YvanUI.ajax.func({
                                                url: '/kg/search/data/info',
                                                method: 'post-json',
                                                data: {
                                                    label: 'gender',
                                                    name: 'disease',
                                                    align: false
                                                }
                                            }).then((res: any) => {
                                                console.log(res.result)
                                                const data = _.map(res.result, (i) => {
                                                    return {value: i, label: i}
                                                })
                                                resolve(data)
                                            }).finally(() => {

                                            })
                                        })
                                    }
                                },
                            },
                            {
                                view: 'select',
                                bind: 'main.gender',
                                rules: [{required: true}],
                                label: '性别',
                                data: [
                                    {value: '0', label: '男性'},
                                    {value: '1', label: '女性'},
                                    {value: '2', label: '不限'}
                                ]
                            },
                            {
                                view: 'select',
                                bind: 'main.version',
                                rules: [{required: true}],
                                label: '版本',
                                data: _.map(Data.versions, (n) => {
                                    return {value: n, label: n}
                                })
                            }
                        ]
                    }
                ]
            }
        ],
        footer: [
            {
                view: 'button',
                text: '确定',
                type: 'primary',
                icon: 'fa fa-save',
                onClick: () => new Promise((resolve, reject) => {
                    this.refs.theform.validateFields().then(() => {
                        let params: any = {
                            "version": this.main.version,
                            "gender": this.main.gender
                        }

                        switch (this.main.nodeType) {
                            case 'group':
                                Object.assign(params, {
                                    "diseases": this.main.diseases_group.split(','),
                                    "symptoms": this.main.symptoms_group.split(','),
                                    "gender": this.main.gender,
                                    "version": this.main.version,
                                })
                                break
                        }

                        YvanUI.ajax.func({
                            url: '/kg/node/insert',
                            method: 'post-json',
                            data: {
                                "label": this.main.nodeType,
                                "params": params
                            }
                        }).then((rest) => {
                            const res: any = rest
                            if (res.code != '0') {
                                YvanUI.showErrorDialog(res.result)
                                reject(res.result)
                            } else {
                                YvanUI.showSuccess('插入成功')
                                Object.assign(this.main, {
                                    guid: res.guid,
                                    nodeValue: '',
                                    symptoms_group: '',
                                    diseases_group: '',
                                });
                                resolve()
                            }
                        })

                    }).catch(e => {
                        YvanUI.showValidateErrorDialog(this, e)
                        reject(e)
                    })
                })
            },
            {
                view: 'button',
                text: '导入',
                type: 'primary',
                icon: 'fa fa-save',
                onClick: () => new Promise((resolve, reject) => {
                    this.refs.theform.validateFields().then(() => {
                        let params: any = {
                            "version": this.main.version,
                            "gender": this.main.gender
                        }

                        switch (this.main.nodeType) {
                            case 'group':
                                Object.assign(params, {
                                    "label": "group",
                                    "guid": this.main.guid,
                                })
                                break
                        }

                        YvanUI.ajax.func({
                            url: '/kg/insert/graph/node',
                            method: 'post-json',
                            data: {
                                "label": this.main.nodeType,
                                "params": params
                            }
                        }).then((rest) => {
                            const res: any = rest
                            if (res.code != '0') {
                                YvanUI.showErrorDialog(res.result)
                                reject(res.result)
                            } else {
                                YvanUI.showSuccess('插入成功')
                                Object.assign(this.main, {
                                    guid: '',
                                    nodeValue: '',
                                    department: '',
                                    sub_department: '',
                                    symptoms_group: '',
                                    diseases_group: '',
                                });
                                resolve()
                            }
                        })

                    }).catch(e => {
                        YvanUI.showValidateErrorDialog(this, e)
                        reject(e)
                    })
                })
            },
            {
                view: 'button',
                text: '清空',
                icon: 'fa fa-times',
                onClick: () => {
                    this.refs.theform.resetFields()
                }
            }
        ],
    }
}